package com.zzh.takeoutapp.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.adapter.order.more.RvListener;
import com.zzh.takeoutapp.model.EvaluateBean;
import com.zzh.takeoutapp.utils.ImageManager;
import com.zzh.takeoutapp.view.folw.FlowTagLayout;
import com.zzh.takeoutapp.view.folw.OnTagClickListener;
import com.zzh.takeoutapp.view.folw.OnTagSelectListener;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/7/27.
 */

public class EvaluateAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener,
        OnTagSelectListener, OnTagClickListener {
    private Context mContext;
    public static final int MIN_CLICK_DELAY_TIME = 1500;
    private long lastClickTime = 0;
    private int TYPE_TOP0 = 0;
    private int TYPE_TOP1 = 1;
    private List<String> dataSource;
    private EvaluateTagAdapter<String> mSizeTagAdapter;
    private ViewOneHolder viewOneHolder;
    private List<EvaluateBean.DataBean.ItemsBean> itemsBeanList;
    private List<EvaluateBean.DataBean.SwitchnavBean> switchnav;
    private EvaluateBean.DataBean.DetailBean detailBeans;
    private MerchantAdapter merchantAdapter;

    public EvaluateAdapter(EvaluateBean list, Context context) {
        super();
        this.mContext = context;
        dataSource = new ArrayList<>();
        itemsBeanList = list.getData().getItems();
        switchnav = list.getData().getSwitchnav();
        detailBeans = list.getData().getDetail();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup container, int type) {
        if (type == TYPE_TOP0) {
            return new ViewOneHolder(LayoutInflater.from(mContext)
                    .inflate(R.layout.include_fragment_evaluate, container, false));
        } else {
            return new ViewHolder(LayoutInflater.from(mContext)
                    .inflate(R.layout.item_evaluate_list, container, false));
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof ViewOneHolder) {
            viewOneHolder = (ViewOneHolder) holder;
            dataSource.clear();
            for (int i = 0; i < switchnav.size(); i++) {
                dataSource.add(switchnav.get(i).getName() + " " + switchnav.get(i).getNum());
            }
            mSizeTagAdapter = new EvaluateTagAdapter<>(mContext);
            mSizeTagAdapter.onlyAddAll(dataSource);
            viewOneHolder.mHistoryFlowLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
            viewOneHolder.mHistoryFlowLayout.setAdapter(mSizeTagAdapter);
            mSizeTagAdapter.notifyDataSetChanged();
            viewOneHolder.mHistoryFlowLayout.setOnTagSelectListener(this);
            viewOneHolder.mHistoryFlowLayout.setOnTagClickListener(this);
            viewOneHolder.tv_all.setOnClickListener(this);
            viewOneHolder.tv_picture.setOnClickListener(this);
            viewOneHolder.tv_avg_score.setText(detailBeans.getAvg_score());
            viewOneHolder.avg_good.setText(detailBeans.getAvg_good() + "%");
            viewOneHolder.rb.setRating(Float.parseFloat(detailBeans.getAvg_peisong()));
            viewOneHolder.rb_view.setRating(Float.parseFloat(detailBeans.getAvg_peisong()));
            viewOneHolder.tv_peisong.setText(detailBeans.getAvg_peisong());
            viewOneHolder.tv_peisong_par.setText(detailBeans.getAvg_peisong());
        } else if (holder instanceof ViewHolder) {
            EvaluateBean.DataBean.ItemsBean itemsBean = itemsBeanList.get(position - 1);
            ImageManager.loadCircleImage(mContext, itemsBean.getFace(), ((ViewHolder) holder).user_head);
            ((ViewHolder) holder).tv_content.setText(itemsBean.getContent());
            ((ViewHolder) holder).tv_reply_time.setText(itemsBean.getReply_time());
            ((ViewHolder) holder).tv_nickname.setText(itemsBean.getNickname());
            ((ViewHolder) holder).tv_pei_time.setText("专送" + itemsBean.getPei_time());
            ((ViewHolder) holder).tv_avg_score.setText("口味:" + detailBeans.getAvg_peisong());
            ((ViewHolder) holder).tv_package.setText("包装:" + itemsBean.getScore());
            ((ViewHolder) holder).tv_avg_peisong.setText("配送:" + detailBeans.getAvg_peisong());
            ((ViewHolder) holder).rb_score.setRating(Float.parseFloat(itemsBean.getScore()));
            List<String> list = new ArrayList<>();
            List<EvaluateBean.DataBean.ItemsBean.CommentPhotosBean> comment_photos = itemsBean.getComment_photos();
            for (int i = 0; i < comment_photos.size(); i++) {
                list.add(comment_photos.get(i).getPhoto());
            }
            merchantAdapter = new MerchantAdapter(mContext, list, new RvListener() {
                @Override
                public void onItemClick(int id, int position) {

                }
            });
            ((ViewHolder) holder).recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
            ((ViewHolder) holder).recyclerView.setAdapter(merchantAdapter);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    long currentTime = System.currentTimeMillis();
                    if (currentTime - MIN_CLICK_DELAY_TIME > lastClickTime) {
                        lastClickTime = System.currentTimeMillis();
                        if (mOnItemClickLitener != null) {
                            holder.itemView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    mOnItemClickLitener.onItemClick(holder.itemView, position - 1);
                                }
                            });
                            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                                @Override
                                public boolean onLongClick(View v) {
                                    mOnItemClickLitener.onItemLongClick(holder.itemView, position - 1);
                                    return true;
                                }
                            });
                        }
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return 1 + itemsBeanList.size();
    }

    @Override
    public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
    }

    @Override
    public void onItemClick(FlowTagLayout parent, View view, int position) {
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int postion);

        void onItemLongClick(View v, int postion);
    }

    /**
     * 自定义条目点击监听
     */
    private HomeFragmentAdapter.OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(HomeFragmentAdapter.OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TOP0;
        } else return TYPE_TOP1;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_all:
                viewOneHolder.tv_all.setTextColor(mContext.getResources().getColor(R.color.white));
                viewOneHolder.tv_picture.setTextColor(mContext.getResources().getColor(R.color.black));
                viewOneHolder.tv_all.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.shape_color_right));
                viewOneHolder.tv_picture.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.shape_color_y_white));
                break;
            case R.id.tv_picture:
                viewOneHolder.tv_all.setTextColor(mContext.getResources().getColor(R.color.black));
                viewOneHolder.tv_picture.setTextColor(mContext.getResources().getColor(R.color.white));
                viewOneHolder.tv_all.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.shape_color_white));
                viewOneHolder.tv_picture.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.shape_color_y));
                break;
        }
    }

    //普通的Holder
    public class ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.user_head)
        ImageView user_head;
        @Bind(R.id.tv_content)
        TextView tv_content;
        @Bind(R.id.tv_nickname)
        TextView tv_nickname;
        @Bind(R.id.tv_reply_time)
        TextView tv_reply_time;
        @Bind(R.id.recyclerView)
        RecyclerView recyclerView;
        @Bind(R.id.tv_pei_time)
        TextView tv_pei_time;
        @Bind(R.id.tv_avg_score)
        TextView tv_avg_score;
        @Bind(R.id.tv_package)
        TextView tv_package;
        @Bind(R.id.tv_avg_peisong)
        TextView tv_avg_peisong;


        @Bind(R.id.rb_score)
        RatingBar rb_score;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class ViewOneHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.rb)
        RatingBar rb;
        @Bind(R.id.rb_view)
        RatingBar rb_view;
        @Bind(R.id.tv_avg_score)
        TextView tv_avg_score;
        @Bind(R.id.avg_good)
        TextView avg_good;
        @Bind(R.id.tv_peisong)
        TextView tv_peisong;
        @Bind(R.id.tv_peisong_par)
        TextView tv_peisong_par;
        @Bind(R.id.tv_all)
        TextView tv_all;//全部
        @Bind(R.id.tv_picture)
        TextView tv_picture;//有图
        @Bind(R.id.history_flow_layout)
        FlowTagLayout mHistoryFlowLayout;


        public ViewOneHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
